﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HomeDMSDomain;

namespace HomeDms
{
    public partial class FindPanelControl : UserControl
    {
        public IList<Document> SearchResult { get; set; }

        public event EventHandler SearchExecuted;
        public event EventHandler SearchCleared;

        public FindPanelControl()
        {
            InitializeComponent();
        }

        private void FindPanelControl_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;
            BindnFindObjectsDataContext db = BindnFindObjectsDataContext.Instance;
            AutoCompleteStringCollection docComplete = new AutoCompleteStringCollection();
            var dq = from doc in db.Documents select doc.Name;
            docComplete.AddRange(dq.ToArray<string>());
            txtDocName.AutoCompleteCustomSource = docComplete;

            AutoCompleteStringCollection tagComplete = new AutoCompleteStringCollection();
            var tq = from tag in db.Tags select tag.Name;
            tagComplete.AddRange(tq.ToArray<string>());
            txtTags.AutoCompleteCustomSource = tagComplete;
            txtTags.DataSource = tagComplete;
            txtTags.SelectedIndex = -1;
            txtTags.SelectedIndexChanged +=new EventHandler(txtTags_SelectedIndexChanged);
        }

        public void ClearSearch()
        {
            txtDocName.Text = "";
            txtTags.SelectedIndex = -1;
            txtTags.Focus();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            BindnFindObjectsDataContext db = BindnFindObjectsDataContext.Instance;

             IList<Document> byNameSearcResult = null;
            if (!string.IsNullOrEmpty(txtDocName.Text))
            {
                //IEnumerable<Document> en = db.Documents.TakeWhile<Document>(d => d.Name.Contains(txtDocName.Text) == true);
                var q = from d in db.Documents where d.Name.Contains(txtDocName.Text) == true select d;
                byNameSearcResult = new List<Document>(q);
            }

            IList<Document> byTagSearchResult = null;
            if (!string.IsNullOrEmpty(txtTags.Text))
            {
                Tag tag = null;
                if (db.Tags.Count<Tag>(t => t.Name == txtTags.Text) > 0)
                {
                    tag = db.Tags.First<Tag>(tg => tg.Name == txtTags.Text);
                    var q = from dt in db.Document_Tags where dt.tag_id == tag.tag_id select dt.Document;
                    byTagSearchResult = new List<Document>(q);
                }
            }

            SearchResult = new List<Document>();

            if (byNameSearcResult != null && byTagSearchResult != null)
            {
                IEnumerable<Document> tmp = byNameSearcResult.Concat<Document>(byTagSearchResult);
                ((List<Document>)SearchResult).AddRange(tmp.Distinct<Document>());
                if (SearchExecuted != null)
                {
                    SearchExecuted(this, EventArgs.Empty);
                }
                return;
            }

            if (byNameSearcResult != null)
            {
                SearchResult = byNameSearcResult;
                if (SearchExecuted != null)
                {
                    SearchExecuted(this, EventArgs.Empty);
                }
                return;
            }

            if (byTagSearchResult != null)
            {
                SearchResult = byTagSearchResult;
                if (SearchExecuted != null)
                {
                    SearchExecuted(this, EventArgs.Empty);
                }
            }
        }

        private void txtDocName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                Search();
        }

        private void txtTags_SelectedIndexChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtTags.SelectedIndex = -1;
            txtDocName.Clear();
            Search();
            if (SearchCleared != null)
            {
                SearchCleared(this, EventArgs.Empty);
            }
        }

        private void lblDocName_Click(object sender, EventArgs e)
        {

        }

    }
}
